package com.bit.trans.controller;

import com.bit.trans.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;

@Slf4j
@RequestMapping("/trans")
@RestController
public class TransController {
    @Autowired
    private UserService userService;

    /**
     * 事务提交
     */
    @Transactional
    @RequestMapping("/r1")
    public String r1(String userName, String password){
        Integer re = userService.insertUser(userName, password);
        log.info("用户注册成功, 影响行数: "+ re);
        return "用户注册成功";
    }

    /**
     * 发生异常, 事务回滚
     */
    @Transactional
    @RequestMapping("/r2")
    public String r2(String userName, String password){
        Integer re = userService.insertUser(userName, password);
        log.info("用户注册成功, 影响行数: "+ re);
        int a= 10/0;
        return "用户注册成功";
    }

    /**
     * 事务提交
     */
    @Transactional
    @RequestMapping("/r3")
    public String r3(String userName, String password){
        Integer re = userService.insertUser(userName, password);
        log.info("用户注册成功, 影响行数: "+ re);
        try {
            int a= 10/0;
        }catch (Exception e){
            log.error("发生异常");
        }
        return "用户注册成功";
    }
    /**
     * 事务回滚
     */
    @Transactional
    @RequestMapping("/r4")
    public String r4(String userName, String password){
        Integer re = userService.insertUser(userName, password);
        log.info("用户注册成功, 影响行数: "+ re);
        try {
            int a= 10/0;
        }catch (Exception e){
            throw e;
        }
        return "用户注册成功";
    }

    /**
     * 事务回滚
     */
    @Transactional
    @RequestMapping("/r5")
    public String r5(String userName, String password){
        Integer re = userService.insertUser(userName, password);
        log.info("用户注册成功, 影响行数: "+ re);
        try {
            int a= 10/0;
        }catch (Exception e){
            log.error("发生异常");
            //手动回滚事务
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }
        return "用户注册成功";
    }

    /**
     * 事务回滚
     */
    @Transactional
    @RequestMapping("/r6")
    public String r6(String userName, String password){
        Integer re = userService.insertUser(userName, password);
        log.info("用户注册成功, 影响行数: "+ re);
        if (true){
            throw new RuntimeException();
        }
        return "用户注册成功";
    }

    /**
     *  事务提交
     */
    @Transactional
    @RequestMapping("/r7")
    public String r7(String userName, String password) throws IOException {
        Integer re = userService.insertUser(userName, password);
        log.info("用户注册成功, 影响行数: "+ re);
        if (true){
            throw new IOException();
        }
        return "用户注册成功";
    }

    /**
     * 事务回滚
     */
    @Transactional(rollbackFor = Exception.class)
    @RequestMapping("/r8")
    public String r8(String userName, String password) throws IOException {
        Integer re = userService.insertUser(userName, password);
        log.info("用户注册成功, 影响行数: "+ re);
        if (true){
            throw new IOException();
        }
        return "用户注册成功";
    }
    /**
     * 事务回滚
     */
    @Transactional(isolation = Isolation.DEFAULT)
    @RequestMapping("/r9")
    public String r9(String userName, String password) throws IOException {
        Integer re = userService.insertUser(userName, password);
        log.info("用户注册成功, 影响行数: "+ re);
        if (true){
            throw new IOException();
        }
        return "用户注册成功";
    }
}
